<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>
        ${member.id == null ? '添加' : '编辑'}管理员
    </title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/system/member/member_form.css">
</head>
<body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">

            <!-- 正文请写在这里 -->
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title" ${member.id == null ? 'style="display:none"' : ''}>
                    <li class="layui-this"><span class="title f18 ml5 mr5">资料编辑</span></li>
                    <c:if test="${sessionScope.newCurrentUser.id == member.id}">
                        <li><span class="title f18 ml5 mr5">修改密码</span></li>
                    </c:if>
                    <c:if test="${sessionScope.newCurrentUser.id != member.id}">
                        <shiro:hasPermission name="admin:member:editPwd">
                            <li><span class="title f18 ml5 mr5">修改密码</span></li>
                        </shiro:hasPermission>
                    </c:if>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <form id="infoForm" class="layui-form mt20" method="post"
                              action="${ctx}/system/member/saveAdmin.do">
                            <input type="hidden" name="id" value="${member.id}"/>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">用户名<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-inline">
                                        <c:choose>
                                            <c:when test="${empty member.id}">
                                                <input class="layui-input" name="username" lay-verify="username" placeholder="用户名" value='${member.username}'/>
                                            </c:when>
                                            <c:when test="${not empty member.id}">
                                                <c:if test="${empty member.username}">
                                                    <input class="layui-input" name="username" lay-verify="username" placeholder="用户名" value='${member.username}'/>
                                                </c:if>
                                                <c:if test="${not empty member.username}">
                                                    <div style="height: 38px; line-height: 38px;">${member.username}</div>
                                                    <input type="hidden" name="username" value='${member.username}'/>
                                                </c:if>
                                            </c:when>
                                        </c:choose>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">昵称<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-inline">
                                        <input class="layui-input" name="realname" lay-verify="realname"
                                               placeholder="昵称" value='${member.realname}'/>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">真实姓名<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-inline">
                                        <input class="layui-input" name="fullname" placeholder="真实姓名" lay-verify="fullname"
                                               value='${member.fullname}'/>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">头像</label>
                                <div class="cover-content">
                                    <input type="hidden" name="logo" id="pic" lay-verify="pic"
                                           value="${(member.logo == null || empty member.logo) ? 'http://txzapp-10052192.image.myqcloud.com/1490783498894':member.logo}"/>
                                    <c:if test="${member == null || empty member.logo}">
                                        <span id="cover-img" class="round-img"
                                              style="background-image:url(http://txzapp-10052192.image.myqcloud.com/1490783498894)"></span>
                                    </c:if>
                                    <c:if test="${member != null && not empty member.logo}">
                                        <span id="cover-img" class="round-img"
                                              style="background-image:url('${member.logo}')"></span>
                                    </c:if>
                                   <a class="layui-btn layui-btn-danger" id="sel_pic_img">+添加头像</a>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">性别<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-inline">
                                        <input type="radio" name="sex" value="1" title="男"
                                        ${member.id == null || member.sex == null || member.sex == '1' ? 'checked="checked"' : ''}
                                        >
                                        <input type="radio" name="sex" value="0" title="女"
                                        ${member.id != null && member.sex == '0' ? 'checked="checked"' : ''}
                                        >
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">手机号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="mobile" lay-verify="mobile" autocomplete="off"
                                               class="layui-input"
                                               value="${member.mobile}"
                                        >
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">QQ号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="qq" class="layui-input" value="${member.qq}"
                                               lay-verify="qq">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">微信号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="wechatNum" class="layui-input"
                                               value="${member.wechatNum}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">公司</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="company" autocomplete="off" class="layui-input"
                                               value="${member.company}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">职位</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="jobTitle" autocomplete="off" class="layui-input"
                                               value="${member.jobTitle}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">行业</label>
                                    <div class="layui-input-inline">
                                        <select lay-verify="industryParent" lay-filter="industryParent">
                                            <option value="">选择行业分类</option>
                                            <c:forEach var="industry" items="${industries}">
                                                <option value="${industry.id}" ${inParent == industry.id?'selected="selected"' : ''}>${industry.name}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                    <div class="layui-input-inline">
                                        <select name="industry" lay-verify="industry" id="industry">
                                            <option value="">选择行业</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">城市</label>
                                    <div class="layui-input-inline">
                                        <select lay-verify="province" lay-filter="province">
                                            <option value="">选择省份/直辖市</option>
                                            <c:forEach var="area" items="${areas}">
                                                <option value="${area.id}" ${arParent == area.id?'selected="selected"' : ''}>${area.name}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                    <div class="layui-input-inline">
                                        <select name="city" lay-verify="city" id="city">
                                            <option value="">选择城市</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label"></label>
                                    <div class="layui-input-inline">
                                        <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit
                                           lay-filter="infoForm">立即提交</a>
                                        <a href="javascript:void(0)" class="layui-btn layui-btn-primary"
                                           onclick="cancelFunction()">取消</a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>

                    <div class="layui-tab-item">
                        <form id="pwdForm" class="layui-form mt20" method="post"
                              action="${ctx}/member/group/updatePwd.do">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label"></label>
                                    <div class="layui-input-inline">
                                        <span style="color: #aaa">密码只能输入字母或数字</span>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">新密码<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-inline">
                                        <input type="password" class="layui-input" name="password"
                                               lay-verify="password" placeholder="新密码"/>
                                        <input type="hidden" name="id" value="${member.id}"/>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">确认密码<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-inline">
                                        <input type="password" class="layui-input" name="repassword"
                                               lay-verify="repassword" placeholder="再次确认密码"/>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label"></label>
                                    <div class="layui-input-inline">
                                        <button class="layui-btn layui-btn-danger" lay-submit lay-filter="pwdForm">
                                            立即修改
                                        </button>
                                        <button type="reset" class="layui-btn layui-btn-primary"
                                                onclick="cancelFunction()">取消
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div><!--底部-->
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>
<script>
    $(function () {
        $('#sel_pic_img').click(function () {
            txz.openSelImg({
                min: 1,
                max: 1,
                info: '建议尺寸：120x120',
                type: '<%=TargetType.LOGO.getCode()%>',
                cb: function (imgs) {
                    $('#cover-img').css('background-image', 'url(' + imgs[0].path + ')');
                    $('#pic').val(imgs[0].path);
                }
            })
        })
        layui.use(['form', 'laydate', 'element'], function () {
            var form = layui.form, laydate = layui.laydate, element = layui.element;

            element.on('tab', function (data) {
            });

            form.on('radio(isExpert)', function (data) {
                if (data.value == "1") {
                    $("#recommendDiv").show();
                } else {
                    $("#recommendDiv").hide();
                }
            });

            //自定义验证规则
            form.verify({
                username: function (value) {
                    if (!value) {
                        return '用户名不能为空';
                    }
                    if (value != "") {
                        if (!util.checkEmail(value) && !util.checkUserName(value)) {
                            return "用户名可以是邮箱或只能以字母开头、可带数字、@和下划线";
                        } else {
                            var isRepeat = txz.checkGroupIsUnique('username', value, '${member.id}');
                            if (undefined == isRepeat) {
                                return "验证用户名异常";
                            } else if (isRepeat) {
                                return "用户名已存在";
                            }
                        }
                    }
                },
                realname: function (value) {
                    if (!util.isValid(value)) {
                        return '请填写昵称';
                    }
                },
                fullname: function (value) {
                    if (!util.isValid(value)) {
                        return '请填写真实姓名';
                    }
                },
                sex: function (value) {
                    if (value == "") {
                        return "请选择性别";
                    }
                },
                mobile: function (value) {
                    if (value != "" && !util.checkMobile(value)) {
                        return "请填写正确格式的手机号码";
                    } else if (value != "" && util.checkMobile(value)) {
                        var isRepeat = txz.checkGroupIsUnique('mobile', value, '${member.id}', 3);
                        if (undefined == isRepeat) {
                            return "验证手机号异常";
                        } else if (isRepeat) {
                            return "手机号已存在";
                        }
                    }
                },
                qq: function (value) {
                    if (value != "" && !util.checkQQ(value)) {
                        return "QQ号格式不正确";
                    }
                },
                password: function (value) {
                    if (value == "") {
                        return "密码不能为空";
                    } else if (!util.checkPassword(value)) {
                        return "密码格式不正确";
                    }
                },
                repassword: function (value) {
                    var pwd = $("#pwdForm [name=password]").val();
                    if (value == "") {
                        return "确认密码不能为空";
                    } else if (!util.checkPassword(value)) {
                        return "确认密码格式不正确";
                    } else if (pwd != value) {
                        return "确认密码与新密码不匹配";
                    }
                },
                oldRefundPassword: function (value) {
                    if (value == "") {
                        return "原退款密码不能为空";
                    } else if (!util.checkPassword(value)) {
                        return "原退款密码格式不正确";
                    }
                },
                newRefundPassword: function (value) {
                    if (value == "") {
                        return "新退款密码不能为空";
                    } else if (!util.checkPassword(value)) {
                        return "新退款密码格式不正确";
                    }
                },
                confirmRefundPassword: function (value) {
                    var pwd = $("#refundPwd [name=newPassword]").val();
                    if (value == "") {
                        return "确认密码不能为空";
                    } else if (!util.checkPassword(value)) {
                        return "确认密码格式不正确";
                    } else if (pwd != value) {
                        return "确认密码与新密码不匹配";
                    }
                }
            });

            function ajaxSubmit(data, url) {
                var isRepeat = false;
                $.ajax({
                    type: 'POST',
                    async: false, // 使用同步的方法
                    data: data,
                    dataType: 'json',
                    success: function (result) {
                        isRepeat = !result;
                    },
                    url: url
                });
                return isRepeat;
            }

            //监听提交
            form.on('submit(infoForm)', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    var action = $("#infoForm").attr("action");
                    $.post(action, $('#infoForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                location.href = "${ctx}/system/member/adminList.do";
                            });
                        } else {
                            util.layerMsgError("提交失败")
                        }
                    })
                });
                return false;
            });

            //监听提交 修改密码
            form.on('submit(pwdForm)', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    var action = $("#pwdForm").attr("action");
                    $.post(action, $('#pwdForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess('密码修改成功', function () {
                                window.location.reload();
                            });
                        } else {
                            util.layerMsgError("密码修改失败")
                        }
                    })
                });
                return false;
            });

            // 城市
            form.on('select(province)', function (data) {
                $("#city").html("");
                var cityId = data.value;
                loadCityData(cityId);
            });

            // 行业
            form.on('select(industryParent)', function (data) {
                $("#industry").html("");
                var industryId = data.value;
                loadIndustryData(industryId);
            });

            if ('${member.city}' != "") {
                loadCityData('${arParent}');
            }

            if ('${member.industry}' != "") {
                loadIndustryData('${inParent}');
            }

            function loadCityData(cityId) {
                $.post("${ctx}/system/member/getCityByParentId.do", {
                    "cityId": cityId
                }, function (data) {
                    var array = new Array();
                    for (var i = 0; i < data.length; i++) {
                        if ('${member.city}' == data[i].id) {
                            array.push("<option value = '" + data[i].id + "' selected='selected'>" + data[i].name + "</option>");
                        } else {
                            array.push("<option value = '" + data[i].id + "'>" + data[i].name + "</option>");
                        }
                    }
                    $("#city").append(array.join(""));
                    form.render('select');
                });
            }

            function loadIndustryData(industryId) {
                $.post("${ctx}/system/member/getIndustryByParentId.do", {
                    "industryId": industryId
                }, function (data) {
                    var array = new Array();
                    for (var i = 0; i < data.length; i++) {
                        if ('${member.industry}' == data[i].id) {
                            array.push("<option value = '" + data[i].id + "' selected='selected' >" + data[i].name + "</option>");
                        } else {
                            array.push("<option value = '" + data[i].id + "'>" + data[i].name + "</option>");
                        }
                    }
                    $("#industry").append(array.join(""));
                    form.render('select');
                });
            }
        });
    })

    function cancelFunction() {
        location.href = "${ctx}/system/member/adminList.do";
    }
</script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '管理员管理',
            href: '${ctx}/system/member/adminList.do'
        },{
            name: '${member.id == null ? '添加' : '编辑'}管理员',
            curr: true
        }],
        btns:[{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</body>
</html>